package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.*;
import com.project.mapper.*;
import com.project.service.IOrderService;
import com.project.util.SnowIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
      @Autowired
      private IOrderMapper orderMapper;
      @Autowired
      private IProductOrderMapper productOrderMapper;
      @Autowired
      private IProductMapper productMapper;
      @Autowired
      private IUserMapper userMapper;
      @Autowired
      private IExpendRecordMapper expendRecordMapper;
      @Override
      public void add(OrderBean orderBean) {
            ExpendRecord expendRecord=new ExpendRecord();
            if(orderBean.getModeOfPayment().equals("预存款支付")){
                  QueryWrapper<UserBean> userQW=new QueryWrapper<>();
                  userQW.eq("userNum",orderBean.getUserNum());
                  UserBean userBean=userMapper.selectOne(userQW);
                  userBean.setMoney(userBean.getMoney()-orderBean.getTotalPrice());
                  userMapper.updateById(userBean);
                  expendRecord.setModeOfPayment("0");
                  orderBean.setOrderStatus(1);
                  expendRecord.setUserNum(orderBean.getUserNum());
                  expendRecord.setMoney(orderBean.getTotalPrice());
                  expendRecordMapper.insert(expendRecord);
                  
            }else {
                  orderBean.setOrderStatus(0);
            }
            
            orderBean.setOrderNum(SnowIdUtils.uniqueLongHex());
            orderMapper.insert(orderBean);
            orderBean.getProductOrders().forEach(n->{
                  n.setOrderNum(orderBean.getOrderNum());
                  productOrderMapper.insert(n);
                  QueryWrapper<ProductBean> qw=new QueryWrapper<>();
                  qw.eq("productNum",n.getProductNum());
                  ProductBean productBean=productMapper.selectOne(qw);
                  productBean.setNum(productBean.getNum()-n.getNum());
                  productMapper.updateById(productBean);
            });
            
            
      }
      
      @Override
      public List<OrderBean> findByDelStatus(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }
      @Override
      public List<OrderBean> findByDelStatus2(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.eq("orderStatus",0);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }
      @Override
      public List<OrderBean> findByDelStatus3(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.ge("orderStatus",1);
            qw.le("orderStatus",2);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }@Override
      public List<OrderBean> findByDelStatus4(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.eq("orderStatus",3);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }
      @Override
      public List<OrderBean> findByDelStatus5(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.eq("orderStatus",4);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }
      @Override
      public List<OrderBean> findByDelStatus6(Long userNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("delStatus",1);
            qw.eq("orderStatus",5);
            qw.eq("userNum",userNum);
            return orderMapper.selectList(qw);
      }
      
      
      @Override
      public OrderBean findByOrderNum(String orderNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("orderNum",orderNum);
            OrderBean orderBean=orderMapper.selectOne(qw);
            QueryWrapper<ProductOrder> qw2 =new QueryWrapper<>();
            qw2.eq("orderNum",orderNum);
            List<ProductOrder> l=productOrderMapper.selectList(qw2);
            l.forEach(n->{
                  QueryWrapper<ProductBean> qw3 =new QueryWrapper<>();
                  qw3.eq("productNum",n.getProductNum());
                  n.setProductBean(productMapper.selectOne(qw3));
            });
            orderBean.setProductOrders(l);
            return orderBean;
      }
      
      @Override
      public Boolean del(String orderNum) {
            QueryWrapper<OrderBean> qw =new QueryWrapper<>();
            qw.eq("orderNum",orderNum);
            OrderBean orderBean=orderMapper.selectOne(qw);
            if(orderBean.getOrderStatus()==3||orderBean.getOrderStatus()==5){
                  orderBean.setDelStatus(0);
                  orderMapper.updateById(orderBean);
                  return true;
            }else {
                  return false;
            }
            
      }
      
      @Override
      public void returnOrder(ReturnOrder returnOrder) {
      
      }
      
      
}
